<?php
namespace app\blog\controller;
use app\blog\model\ShopModel;
use app\blog\model\ShopTypeModel;
use app\blog\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

class ShopController extends AdminBaseController
{
    //店铺列表
    public function index(){
        //分页、搜索
        $where     =array();
        $params    =$this->request->param();
        $s         = new ShopModel();
        $list      = $s->index($params);
        $list->appends($params);
        $this->assign("page", $list->render());
        $this->assign('list',$list);
        return $this->fetch();
    }
    
    //新增店铺
    public function add(){
        if($this->request->param()){
            $res  = $this->request->param();
            if(empty($res['shop_name'])){
                $this->error('店铺名称必填');
            }
            if(empty($res['agency_id'])){
                $this->error('请选择代理');
            }else{
                if(empty($res['stype_id'])){
                    $this->error('请选择类别');
                }
            }

            //判断店铺是否存在
            $s         = new ShopModel();
            $checkinfo = $s->contrast($res['shop_name']);
            if($checkinfo){
                $this->error('店铺名称已存在');
            }
            $data=array(
                're_guid'    =>guid(),
                'shop_name'  =>trim(strip_tags($res['shop_name'])),
                'agency_id'  =>$res['agency_id'],
                'stype_id'   =>$res['stype_id'],
                'address'    =>$res['address'],
                'c_time'     =>time()
            );
            $result = $s->addPost($data);
            if($result == 1){
                $this->success('新增成功', url('shop/index'));
            }else{
                $this->error('新增失败');
            }
        }else{
            $id = cmf_get_current_admin_id();
            $this->assign('id',$id);//判断是否是admin 如果显示所有代理，否只显示登录代理
            //获取代理商
            $u           = new UserModel();
            $agency_list = $u->getAll();
            $this->assign('agency_list',$agency_list);
            $ret1 = $u->getUsermsgs($id);
            $this->assign('ret1',$ret1);
            //通过代理的信息获取代理下店铺类型的信息
            if($id != 1){
                $sy     = new ShopTypeModel();
                $res= $sy->getShoptype($ret1['user_guid']);
                $this->assign('res',$res);
            }

            return $this->fetch();
        }
    }
    /**
     * @return mixed
     * 获取指定代理商下的店铺类别
     */
    public function getShoptype(){
        $id     = $this->request->param('id');
        $sy     = new ShopTypeModel();
        $result = $sy->getShoptype($id);
        return $result;
    }
    
    
    //编辑店铺
    public function edit(){
        $res  = $this->request->param();
        if($this->request->isPost()){
            $data=array(
                'shop_name'   =>trim(strip_tags($res['shop_name'])),
                'agency_id'   =>$res['agency_id'],
                'stype_id'    =>$res['stype_id'],
                'address'     =>$res['address'],
            );
            $result=DB::name('shop')->where(array('id'=>$res['id']))->update($data);
            if($result){
                $this->success('编辑成功', url("shop/index"));
            }else{
                $this->error('编辑失败');
            }
        }else{
            //获取代理商
            $u           = new UserModel();
            $agency_list = $u->getAll();
            $this->assign('agency_list',$agency_list);
            $s           = new ShopModel();
            $info        = $s->shopOne($res['id']);
            $this->assign('info',$info);
            $st = new ShopTypeModel();
            $type_list = $st->getShoptype($info['agency_id']);
            $this->assign('type_list',$type_list);

            $id = cmf_get_current_admin_id();
            $this->assign('id',$id);//判断是否是admin 如果显示所有代理，否只显示登录代理

            return $this->fetch();
        }
    }
    
    
    //删除店铺
    public function delete(){
        //判断可以删除的条件
        $id  = $this->request->param('id');
        $check=Db::name('goods')->where(array('shop_id'=>$id))->find();
        if($check){
            $this->error('店铺下有数据，不可删除');
        }else{
            $status = Db::name('shop')->where(['re_guid'=>$id])->delete();
            if($status){
                $this->success('删除成功！');
            }else{
                $this->error("删除失败！");
            }
        }
    }
	    //判断店铺状态 未激活
    public function ban(){
        $id = $this->request->param('id');
        if(!empty($id)){
            $data = new ShopModel();
            $result = $data->ShopBan($id);
            if($result !== false){
                $this->success("店铺关闭成功！");
            }else{
                $this->error('店铺关闭失败！');
            }
        }else{
            $this->error('数据传入失败！');
        }
    }
    //判断店铺状态 激活
    public function cancelBan()
    {
        $id = $this->request->param('id');
        if (!empty($id)) {
            $data = new ShopModel();
            $result = $data->ShopCancelBan($id);
            if ($result !== false) {
                $this->success("店铺激活成功！");
            } else {
                $this->error('店铺激活失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
    /**
     * 我的店铺 适用于代理商和店长
     */
    public function ownShop(){
        $id = cmf_get_current_admin_id();
        $this->assign('id',$id);
        //获取我的店铺信息
        $s = new ShopModel();
        $rest = $s->ownShop();
        $this->assign('rest',$rest);
        return  $this->fetch();
    }
    /**
     * 我的店铺编辑页面
     */
    public function ownedit(){
        $id = $this->request->param('id');
        //根据店铺唯一标识获取店铺
        $s = new ShopModel();
        $rest  = $s->shopOne($id);
        $this->assign('rest',$rest);
        return $this->fetch();
    }
    /**
     * 我的店铺编辑
    */
    public function ownPost(){
        $param = $this->request->param();
        $s = new ShopModel();
        $rest = $s->ownPost($param);
        if($rest != false){
            $this->success('操作成功','Shop/ownShop');
        }else{
            $this->error('操作失败');
        }
    }
}